<?php

/**
 * Plugin Name: Falcon5Translate - Multilingual
 * Plugin URI:  http://falcon5.com/zh/
 * Description: Makes your website <strong>multilingual</strong> and available to the world using Falcon5. For support visit <a href="https://wordpress.org/support/plugin/falcon5translate">Falcon5 Support</a>.
 * Author: Falcon5 Translate team
 * Requires PHP: 7.0
 * Requires at least: 5.6
 * Tested up to: 6.2
 */

use Doctrine\Common\Annotations\AnnotationRegistry;
use falcon5\inc\Application;
use falcon5\inc\Handlers\Activate;
use falcon5\inc\Handlers\Deactivate;
use falcon5\inc\Infrastructure\Services\MigrateService;

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if (file_exists(dirname(__FILE__) . '/vendor/autoload.php')) {
    require_once dirname(__FILE__) . '/vendor/autoload.php';
}

// Register Doctrine annotations, special handling for PHP 8.0
if (PHP_VERSION_ID >= 80000) {
    // PHP 8.0+ needs explicit annotation namespace registration
    if (class_exists('Doctrine\Common\Annotations\AnnotationRegistry')) {
        // Register class loader
        AnnotationRegistry::registerLoader('class_exists');
        
        // Explicitly register hollisho annotation namespace
        if (method_exists(AnnotationRegistry::class, 'registerAutoloadNamespace')) {
            AnnotationRegistry::registerAutoloadNamespace(
                'hollisho\httpclient\Annotations', 
                dirname(__FILE__) . '/vendor/hollisho/http-client/src/Annotations'
            );
        }
    }
} else {
    // PHP 7.x
    if (method_exists(AnnotationRegistry::class, 'registerLoader')) {
        AnnotationRegistry::registerLoader('class_exists');
    }
}

/**
 * Get the plugin's MigrateService instance
 * 
 * @return MigrateService Migration service instance
 */
function falcon5_get_migrate_service() {
    static $migrateService = null;
    if ($migrateService === null) {
        $migrateService = new MigrateService();
    }
    return $migrateService;
}

if (class_exists(Application::class)) {
    add_action( 'plugins_loaded', [Application::class, 'registerInfrastructureService'], 1 );
    add_action( 'plugins_loaded', [Application::class, 'registerService'], 5 );
    
    // Add migration check hook after services are registered
    add_action( 'plugins_loaded', function() {
        falcon5_get_migrate_service()->checkMigrations();
    }, 10 );

    add_action( 'setup_theme', function() {
        falcon5_get_migrate_service()->themeLoadere();
    }, 10 );
    //
    add_filter("fc5_translation_body",function($body){
        //Translation result(html|json) 
        //return $body;
    });

    add_action('fc5_translation_start',function($ages){
        //var_dump($ages);
    });
    add_action('fc5_translation_complete',function($ages){
        //var_dump($ages);
    });
    add_action('fc5_translation_failure',function($ages){
        //var_dump($ages);
    });
 }

register_activation_hook(__FILE__, function () {
    // The Activate handler already performs migration
    falcon5_app(Activate::class)->handler();
});

register_deactivation_hook(__FILE__, function () {
    falcon5_app(Deactivate::class)->handler();
});

